package com.lanou.projecta.news.jokes;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.projecta.R;

import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/22.
 */
public class JokeAdapter extends BaseAdapter {
    String text = null;
    private Context context;
    private List<JokesBase.段子Entity> base;
    private RequestQueue queue;

    public JokeAdapter(Context context, List<JokesBase.段子Entity> base) {
        queue = Volley.newRequestQueue(context);
        this.context = context;
        this.base = base;
    }

    public void getImage(NetworkImageView imageView, String url) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        imageView.setImageUrl(url, imageLoader);

    }

    @Override
    public int getCount() {
        if (base != null) {
            return base.size();
        } else return 0;
    }

    @Override
    public Object getItem(int i) {
        if (base != null) {
            return base.get(i);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        if (base != null) {
            return i;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        String url = null;
        Handler1 handler1 = null;
        Handler2 handler2 = null;
        if (base.get(i).getImgsrc() == null) {
            view = LayoutInflater.from(context).inflate(R.layout.jokes_item, null);
            handler1 = new Handler1();
            handler1.body = (TextView) view.findViewById(R.id.joke_body);
            handler1.diggCount = (TextView) view.findViewById(R.id.jokes_digg_count);
            handler1.share = (ImageView) view.findViewById(R.id.jokes_share);
            handler1.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    text = base.get(i).getDigest();
                    showShare();
                }
            });
            view.setTag(handler1);
        } else {
            url = base.get(i).getImgsrc();
            view = LayoutInflater.from(context).inflate(R.layout.jokes_img_item, null);
            handler2 = new Handler2();
            handler2.body = (TextView) view.findViewById(R.id.jokes_img_body);
            handler2.diggCount = (TextView) view.findViewById(R.id.jokes_img_digg_count);
            handler2.share = (ImageView) view.findViewById(R.id.jokes_img_share);
            handler2.img = (NetworkImageView) view.findViewById(R.id.jokes_img);
            handler2.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    text = base.get(i).getDigest();
                    showShare();
                }
            });
        }

        if (base != null) {
            if (base.get(i).getImgsrc() == null) {
                handler1.body.setText(base.get(i).getDigest());
                handler1.diggCount.setText(String.valueOf(base.get(i).getUpTimes()));
            } else {
                handler2.body.setText(base.get(i).getDigest());
                handler2.diggCount.setText(String.valueOf(base.get(i).getUpTimes()));
                getImage(handler2.img, url);

            }


        }


        return view;
    }

    class Handler1 {
        TextView body;
        TextView diggCount;

        ImageView share;

    }

    class Handler2 {
        TextView body;
        TextView diggCount;
        ImageView share;
        NetworkImageView img;

    }
    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(context.getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
     //   oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText(text);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
       // oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(context);
    }
}
